var fs = require('fs');
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'datalisheng'
});
 
connection.connect();

var infoJSON = {status:'',data:''};

exports.login = (req,res) => {
	 var response = {
       "tel":req.body.tel,
       "password":req.body.password
   };
  connection.query('SELECT * from user where tel = '+req.body.tel+' and password = '+req.body.password, function (error, results, fields) {
	  if (error) throw error;
	  if(results[0]){
	  	infoJSON.status = '0';
	  	infoJSON.data = results[0];
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '登录失败，用户名或密码错误！'
	  }
	  res.send(infoJSON);
	});
 //  res.end(JSON.stringify(response));
}

exports.fetchUserList = (req,res) => {
	connection.query('SELECT * from user', function (error, results, fields) {
	  if (error) throw error;
	  if(results[0]){
	  	infoJSON.status = '0';
	  	infoJSON.data = results;
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '无数据'
	  }
	  res.send(infoJSON);
	});
}

exports.dynamicList = (req,res) => {
	connection.query('SELECT * from dynamic', function (error, results, fields) {
	  if (error) throw error;
	  if(results[0]){
	  	infoJSON.status = '0';
	  	infoJSON.data = results;
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '无数据'
	  }
	  res.send(infoJSON);
	});
}

exports.userDepament = (req,res) => {
	 var response = {
       "user_id":req.body.userId,
       "depament":req.body.depamentId
   };
  connection.query('UPDATE user set depament = '+response.depament+' where user_id = '+response.user_id, function (error, results, fields) {
	  if (error) throw error;
	  if(results.affectedRows === 1){
	  	infoJSON.status = '0';
	  	infoJSON.data = '修改成功！';
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '修改失败！'
	  }
	  res.send(infoJSON);
	});
 //  res.end(JSON.stringify(response));
}

exports.addProject = (req,res) => {
	 var response = {
       "title":req.body.title,
       "name":req.body.name,
       "user":req.body.user,
       "task":req.body.task,
       "color":req.body.color,
       "initial":req.body.initial,
       "process":req.body.process,
       "target":req.body.target,
       "startDate":req.body.startDate,
       "endDate":req.body.endDate,
       "adopt":req.body.adopt,
       "state":req.body.state,
       "data":req.body.data === undefined ? '' : req.body.data ,
       "capitalAll":req.body.capitalAll,
       "capitalIng":req.body.capitalIng,
   };
  connection.query('INSERT INTO project(title,name,user,task,color,initial,process,target,startDate,endDate,adopt,state,data,capitalAll,capitalIng) VALUES("'+response.title+'","'+response.name+'","'+response.user+'","'+response.task+'","'+response.color+'",'+response.initial+','+response.process+','+response.target+',"'+response.startDate+'","'+response.endDate+'","'+response.adopt+'","'+response.state+'","'+response.data+'","'+response.capitalAll+'","'+response.capitalIng+'")', function (error, results, fields) {
	  if (error) throw error;
	  if(results.affectedRows === 1){
	  	infoJSON.status = '0';
	  	infoJSON.data = '新增成功！';
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '新增失败！'
	  }
	  res.send(infoJSON);
	});
 //  res.end(JSON.stringify(response));
}

exports.projectList = (req,res) => {
	connection.query('SELECT * from project', function (error, results, fields) {
	  if (error) throw error;
	  if(results[0]){
	  	infoJSON.status = '0';
	  	infoJSON.data = results;
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '无数据'
	  }
	  res.send(infoJSON);
	});
}

exports.appProject = (req,res) => {
	 var response = {
       "project_id":req.body.project_id,
       "recovery":req.body.recovery,
       "app":req.body.app,
       "capitalAll":req.body.capitalAll,
			 "capitalIng":req.body.capitalIng,
   };
    var all = response.capitalAll;
    var ing = response.capitalIng;
   if(response.recovery === '0'){
   	all = all - ing;
		ing = 0;
   }else if(response.recovery === '2'){
			ing = parseInt(response.capitalIng)-parseInt(response.app);
   }else{
   	all = parseInt(response.capitalAll)+parseInt(response.app);
    ing = parseInt(response.capitalIng)+parseInt(response.app);
   }
  connection.query('UPDATE project set capitalAll = '+all+',capitalIng = '+ing+' where project_id = '+response.project_id, function (error, results, fields) {
	  if (error) throw error;
	  if(results.affectedRows === 1){
	  	infoJSON.status = '0';
	  	infoJSON.data = '修改成功！';
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '修改失败！'
	  }
	  res.send(infoJSON);
	});
}

exports.infoProject = (req,res) => {
	 var response = {
       "project_id":req.body.project_id,
   };
  connection.query('select * from project where project_id = '+response.project_id, function (error, results, fields) {
	  if (error) throw error;
	  if(results[0]){
	  	infoJSON.status = '0';
	  	infoJSON.data = results[0];
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '无数据'
	  }
	  res.send(infoJSON);
	});
}

exports.updataProject = (req,res) => {
	 var response = {
       "project_id":req.body.project_id,
       'user':req.body.user
   };
  connection.query('UPDATE project set user = "'+response.user+'" where project_id = '+response.project_id, function (error, results, fields) {
	  if (error) throw error;
	  if(results.affectedRows === 1){
	  	infoJSON.status = '0';
	  	infoJSON.data = '修改成功！';
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '修改失败！'
	  }
	  res.send(infoJSON);
	});
}

exports.updataStateProject = (req,res) => {
	 var response = {
       "project_id":req.body.project_id,
       'state':req.body.state
   };
  connection.query('UPDATE project set state = "'+response.state+'" where project_id = '+response.project_id, function (error, results, fields) {
	  if (error) throw error;
	  if(results.affectedRows === 1){
	  	infoJSON.status = '0';
	  	infoJSON.data = '修改成功！';
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '修改失败！'
	  }
	  res.send(infoJSON);
	});
}

exports.updataProcessProject = (req,res) => {
	 var response = {
       "project_id":req.body.project_id,
       'process':req.body.process
   };
  connection.query('UPDATE project set process = "'+response.process+'" where project_id = '+response.project_id, function (error, results, fields) {
	  if (error) throw error;
	  if(results.affectedRows === 1){
	  	infoJSON.status = '0';
	  	infoJSON.data = '修改成功！';
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '修改失败！'
	  }
	  res.send(infoJSON);
	});
}

exports.addDynamic = (req,res) => {
	 var response = {
       "project_id":req.body.project_id,
       "name":req.body.name,
       "user_id":req.body.user_id,
       "action":req.body.action,
       "flag":req.body.flag,
       "dynamic_date":req.body.dynamic_date,
       "title":req.body.title,
       "addNum":req.body.addNum,
       "capital":req.body.capital,
       "report":req.body.report
   };

  connection.query('INSERT INTO dynamic(project_id,name,user_id,action,flag,dynamic_date,title,addNum,capital,report) VALUES("'+response.project_id+'","'+response.name+'","'+response.user_id+'","'+response.action+'","'+response.flag+'","'+response.dynamic_date+'","'+response.title+'",'+response.addNum+',"'+response.capital+'","'+response.report+'")', function (error, results, fields) {
	  if (error) throw error;
	  if(results.affectedRows === 1){
	  	infoJSON.status = '0';
	  	infoJSON.data = '新增成功！';
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '新增失败！'
	  }
	  res.send(infoJSON);
	});
 //  res.end(JSON.stringify(response));
}

exports.addUser = (req,res) => {
	 var response = {
       "tel":req.body.tel,
       "name":req.body.name,
       "password":req.body.password,
       "depament":req.body.depament,
       "gender":req.body.gender,
       "birthdate":req.body.birthdate,
       "email":req.body.email,
       "position":req.body.position
   };
  connection.query('INSERT INTO user(tel,name,password,depament,gender,birthdate,email,position) VALUES("'+response.tel+'","'+response.name+'","'+response.password+'","'+response.depament+'","'+response.gender+'","'+response.birthdate+'","'+response.email+'","'+response.position+'")', function (error, results, fields) {
	  if (error) throw error;
	  if(results.affectedRows === 1){
	  	infoJSON.status = '0';
	  	infoJSON.data = '注册成功！';
	  }else{
	  	infoJSON.status = '1';
	  	infoJSON.data = '注册失败！'
	  }
	  res.send(infoJSON);
	});
 //  res.end(JSON.stringify(response));
}

exports.fetchDorm = (req,res) => {
	connection.query('SELECT * from user', function (error, results, fields) {
	  if (error) throw error;
	  res.send(results);
	});
}
exports.fetchRorm = (req,res) => {
	
}


